﻿using System.Collections.Generic;
using System.Linq;
using Cms.Domain.Comparers;

namespace Cms.Domain
{
    public abstract class ContentSetContainer
    {
        private IDictionary<int, ContentSet> _contentSets;
        private string _description;
        private int _id;
        private string _name;
        private Site _site;

        protected ContentSetContainer()
        {
            _contentSets = new Dictionary<int, ContentSet>();
        }
        
        public virtual int ID
        {
            get { return _id; }
            set { _id = value; }
        }
        
        public virtual string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        
        public virtual string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        
        public virtual Site Site
        {
            get { return _site; }
            set { _site = value; }
        }

        public virtual IDictionary<int, ContentSet> ContentSets
        {
            get { return _contentSets; }
            set { _contentSets = value; }
        }

        public abstract string GetPublishedContentHtml(string areaID, int localeID);
        public abstract string GetPublishedContentTitle(string areaID, int localeID);
        public abstract string GetContentHtml(string areaID, int localeID, string timestamp);
        public abstract string GetContentTitle(string areaID, int localeID, string timestamp);
        public abstract void AddContentLink(string areaID, Content content);
        public abstract void PublishContentVersion(int localeID, ContentTranslationVersion ctv);
        
        public virtual ContentSet GetContentSet(Locale locale)
        {
            if (!_contentSets.ContainsKey(locale.ID))
                _contentSets.Add(locale.ID, CreateContentSet(locale));
            return _contentSets[locale.ID];
        }

        public virtual IList<ContentTranslationVersion> GetContentVersions(int localeID)
        {
            if (!_contentSets.ContainsKey(localeID))
                return new List<ContentTranslationVersion>();
            List<ContentTranslationVersion> versions = _contentSets[localeID].TranslationVersions.ToList();
            versions.Sort(new ContentTranslationVersionComparer(SortDirection.Descending, ContentTranslationVersionSortMode.Timestamp));
            return versions;
        }
        
        public virtual bool HasNewerVersionThanPublished(int localeID)
        {
            if (!_contentSets.ContainsKey(localeID))
                return false;
            ContentSet cs = _contentSets[localeID];
            if (cs.TranslationVersions.Count == 0)
                return false;
            IEnumerable<ContentTranslationVersion> versions = from tv in cs.TranslationVersions
                                                              where cs.PublishedVersion != null &&
                                                                    long.Parse(tv.Timestamp) >
                                                                    long.Parse(cs.PublishedVersion.Timestamp)
                                                              select tv;
            return versions.Count() > 0;
        }
        
        public abstract Content GetContent(string areaID);

        protected abstract ContentSet CreateContentSet(Locale locale);
    }
}